var Myaddress = decodeURI(window.location.href).split('=');
var MyaddressDetail = Myaddress[4];
var provinceId;
var cityId;
var countyId;
//是否获取到地区
$(function(){
	if(MyaddressDetail==undefined||MyaddressDetail==null||MyaddressDetail==""){
		$('#address').val('');
		provinceId = '';
		cityId = '';
		countyId = '';
	}else{
		$('#address').val(MyaddressDetail);
		provinceId = Myaddress[1].split('&')[0];
		cityId = Myaddress[2].split('&')[0];
		countyId = Myaddress[3].split('&')[0];
	}
})

//是否将此地址设为默认
$(function(){
	$('#mySwitch').click(function() {
		if($(this).hasClass('mui-active')){
			$(this).removeClass('mui-active')
		}else{
			$(this).addClass('mui-active')
		}
	})
})


//显示原始地址
$(function(){
	$.ajax({
		url:'/user/getAddress.json',
		dataType:'json',
		type:'post',
		data:{
			token:getItem('token'),
		},
		success:function(data){
			if(data.code==0){
				mui.toast(data.msg);
			}
			if(data.code==-1){
				window.location.href="login.html";
			}
			if(data.code==1){
				var data =data.datas;
				$('#detail').val(data.address.detail);
			}
		}
	})
})

//点击所在地区
$('.diqu').click(function(){
	window.location.href="address-detail.html";
})

//点击保存
$('.baocun').click(function(){
	$.ajax({
		url:'/user/addOrUpdateAddress.json',
		dataType:'json',
		data:{
			provinceId:provinceId,
			cityId:cityId,
			countyId:countyId,
			token:getItem('token'),
			detail:$('#detail').val(),
		},
		type:'post',
		success:function(data){
			if(data.code==-1){
				window.location.href="login.html";
			}
			if(data.code==0){
				mui.toast(data.msg);
			}
			if(data.code==1){
				window.location.href="set.html";
			}
		}
	})
})
